package com.dlc.garbagerecyclebox.bean;


import com.dlc.commonbiz.base.serialport.utils.ByteUtil;
import com.dlc.garbagerecyclebox.GarbageRecyclerBoxProtocol;

/**
 * @author :      fangbingran
 * @aescription : 投递信息
 * @date :        2019/08/01  20:02
 */
public class DeliveryInfoBean extends DeviceInfoBean {
    /**
     * 执行动作
     */
    private byte executeAction;
    /**
     * 量 kg
     */
    private float amount;


    public DeliveryInfoBean(DeviceInfoBean deviceInfoBean) {
        super(deviceInfoBean);
    }

    /**
     * 副柜：开门前称重
     *
     * @return
     */
    public boolean isWeighingBeforeOpenDoor() {
        return executeAction == GarbageRecyclerBoxProtocol.DeliveryCmd.CMD_WEIGHING_BEFORE_OPEN_DOOR;
    }

    /**
     * 开上舱门
     *
     * @return
     */
    public boolean isOpenHatchDoor() {
        return executeAction == GarbageRecyclerBoxProtocol.DeliveryCmd.CMD_OPEN_HATCH_DOOR;
    }

    /**
     * 主柜：等待第一光电触发（投递超时/结束投递命令）,副柜：等待投递超时/结束投递命令
     *
     * @return
     */
    public boolean isFirstPhotoelectricTrigger() {
        return executeAction == GarbageRecyclerBoxProtocol.DeliveryCmd.CMD_FIRST_PHOTOELECTRIC_TRIGGER;
    }

    /**
     * 主柜：传送至第二光电
     *
     * @return
     */
    public boolean isTwoPhotoelectricTrigger() {
        return executeAction == GarbageRecyclerBoxProtocol.DeliveryCmd.CMD_TWO_PHOTOELECTRIC_TRIGGER;
    }

    /**
     * 主柜：识别通过传送至第三光电
     *
     * @return
     */
    public boolean isThreePhotoelectricTrigger() {
        return executeAction == GarbageRecyclerBoxProtocol.DeliveryCmd.CMD_THREE_PHOTOELECTRIC_TRIGGER;
    }

    /**
     * 主柜：识别失败返回至第一光电
     *
     * @return
     */
    public boolean isReturnFirstPhotoelectricTrigger() {
        return executeAction == GarbageRecyclerBoxProtocol.DeliveryCmd.CMD_RETURN_FIRST_PHOTOELECTRIC_TRIGGER;
    }

    public boolean isConveyorBeltOnePhotoelectric() {
        return getSensorStatusBean() != null && getSensorStatusBean().isConveyorBeltOnePhotoelectric();
    }


    public boolean isConveyorBeltTwoPhotoelectric() {
        return getSensorStatusBean() != null && getSensorStatusBean().isConveyorBeltTwoPhotoelectric();
    }


    public boolean isConveyorBeltThreePhotoelectric() {
        return getSensorStatusBean() != null && getSensorStatusBean().isConveyorBeltThreePhotoelectric();
    }

    public float getVerticalRangingHeight() {
        if (getSensorStatusBean() != null) {
            return getSensorStatusBean().getVerticalRangingHeight();
        }
        return 0;
    }

    /**
     * 主柜：拍照识别
     *
     * @return
     */
    public boolean isPhotoRecognition() {
        return executeAction == GarbageRecyclerBoxProtocol.DeliveryCmd.CMD_PHOTO_RECOGNITION;
    }

    /**
     * 主柜：等待取走异常饮料瓶（30秒超时）
     *
     * @return
     */
    public boolean isWaitingRemovalException() {
        return executeAction == GarbageRecyclerBoxProtocol.DeliveryCmd.CMD_WAITING_REMOVAL_EXCEPTION;
    }

    /**
     * 0x38：主柜：等待取走异常饮料瓶超时直接入桶（不计数）
     *
     * @return
     */
    public boolean isWaitingRemovalException2() {
        return executeAction == GarbageRecyclerBoxProtocol.DeliveryCmd.CMD_WAITING_REMOVAL_EXCEPTION2;
    }

    /**
     * 关上舱门
     *
     * @return
     */
    public boolean isCloseHatchDoor() {
        return executeAction == GarbageRecyclerBoxProtocol.DeliveryCmd.CMD_CLOSE_HATCH_DOOR;
    }

    /**
     * 0x61：关门夹手触发第一次回走
     *
     * @return
     */
    public boolean isFirstCloseDoorClampHand() {
        return executeAction == GarbageRecyclerBoxProtocol.DeliveryCmd.CMD_FIRST_CLOSE_DOOR_CLAMP_HAND;
    }

    /**
     * 0x62：关门夹手触发第一次重新关门
     *
     * @return
     */
    public boolean isClampHandAgainCloseDoor() {
        return executeAction == GarbageRecyclerBoxProtocol.DeliveryCmd.CMD_CLAMP_HAND_AGAIN_CLOSE_DOOR;
    }

    /**
     * 0x63：关门夹手触发第二次回走并结束
     *
     * @return
     */
    public boolean isTwoCloseDoorClampHand() {
        return executeAction == GarbageRecyclerBoxProtocol.DeliveryCmd.CMD_TWO_CLOSE_DOOR_CLAMP_HAND;
    }

    /**
     * 副柜：关门后称重
     *
     * @return
     */
    public boolean isWeighingAfterCloseDoor() {
        return executeAction == GarbageRecyclerBoxProtocol.DeliveryCmd.CMD_WEIGHING_AFTER_CLOSE_DOOR;
    }

    /**
     * 投递完成
     *
     * @return
     */
    public boolean isDeliveryCompleted() {
        return executeAction == GarbageRecyclerBoxProtocol.DeliveryCmd.CMD_DELIVERY_COMPLETED;
    }

    public void setExecuteAction(byte executeAction) {
        this.executeAction = executeAction;
    }

    public float getAmount() {
        return amount;
    }

    public void setAmount(float amount) {
        this.amount = amount;
    }

    @Override
    public String toString() {
        return "投递信息 ,执行命令::" + ByteUtil.bytes2HexStr(new byte[]{executeAction}) + "" + ",说明:" + geExplain() + ",结果:" + getDeviceNumberExplain() + "," + super.toString();
    }


    private String getDeviceNumberExplain() {
        if (isMainCabinet()) {
            return "本次投递饮料瓶数量:" + amount;
        }
        return "本次投递重量:" + amount;
    }

    private String geExplain() {
        if (isWeighingBeforeOpenDoor()) {
            return "副柜：开门前称重";
        }
        if (isOpenHatchDoor()) {
            return "开上舱门";
        }
        if (isTwoPhotoelectricTrigger()) {
            return "主柜：传送至第二光电";
        }
        if (isThreePhotoelectricTrigger()) {
            return "主柜：识别通过传送至第三光电";
        }
        if (isReturnFirstPhotoelectricTrigger()) {
            return "主柜：识别失败返回至第一光电";
        }
        if (isWaitingRemovalException()) {
            return "主柜：等待取走异常饮料瓶（30秒超时）";
        }
        if (isWaitingRemovalException2()) {
            return "主柜：等待取走异常饮料瓶超时直接入桶（不计数）";
        }
        if (isCloseHatchDoor()) {
            return "关上舱门";
        }
        if (isFirstCloseDoorClampHand()) {
            return "关门夹手触发第一次回走";
        }
        if (isClampHandAgainCloseDoor()) {
            return "关门夹手触发第一次重新关门";
        }
        if (isTwoCloseDoorClampHand()) {
            return "关门夹手触发第二次回走并结束";
        }
        if (isWeighingAfterCloseDoor()) {
            return " 副柜：关门后称重";
        }
        if (isDeliveryCompleted()) {
            return " 投递完成";
        }
        if (isFirstPhotoelectricTrigger()) {
            if (isMainCabinet()) {
                return "主柜：等待第一光电触发（投递超时/结束投递命令）";
            } else {
                return "副柜：等待投递超时/结束投递命令";
            }

        }
        if (isPhotoRecognition()) {
            return "主柜：拍照识别";

        }
        return "未知状态:";
    }

    @Override
    public DeliveryInfoBean clone() {
        return (DeliveryInfoBean) super.clone();
    }
}
